var baseUrl = require('../../utils/urllink.js')
var app = getApp()
Page({
  data: {
    showTopTips: false,
    showTopTipsText: '',

    countryCodeIndex: 0,
    countryIndex: 0,
    accountIndex: 0,
    isAgree: false,
    files: [],
    files2: [],
    files3: [],

    images1:'',
    images2: '',
    images3: '',
    hostimgs: app.globalData.hostimgs,
    dianpuname:'',
    dianpujieshao: '',
    dianpuxingming: '',
    dianpuphone: '',
    dianpuqq: '',
    dianputel: '',
    dianpuaddress: '',
    wx_code:'',

  },

  onLoad:function(){
    var that = this
    //请求 查询开店数据
    wx.request({
      url: baseUrl +'weapp/Store/store_data.html',
      data: {
        user_id: app.globalData.user_id,
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      }, // 设置请求的 header
      success: function (res) {
        console.log(res.data)
        that.setData({
          dianpuname: res.data.store_name,
          dianpujieshao: res.data.store_desc,
          dianpuxingming: res.data.user_name,
          dianpuphone: res.data.store_phone,
          dianpuqq: res.data.store_qq,
          dianputel: res.data.service_phone,
          dianpuaddress: res.data.store_address,
          wx_code: res.data.wx_code,

          images2: that.data.hostimgs + res.data.store_logo,
          images3: that.data.hostimgs + res.data.store_avatar,
          images1: that.data.hostimgs + res.data.store_cert,

        })
        if (!(res.data.store_cert == null || res.data.store_cert == '')){
          that.setData({
            files: that.data.files.concat(that.data.hostimgs + res.data.store_cert),
          })
        }
        if (!(res.data.store_logo == null || res.data.store_logo == '')) {
          that.setData({
            files2: that.data.files2.concat(that.data.hostimgs + res.data.store_logo),
          })
        }
        if (!(res.data.store_avatar == null || res.data.store_avatar == '')) {
          that.setData({
            files3: that.data.files3.concat(that.data.hostimgs + res.data.store_avatar),
          })
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  },

  //表单提交
  formSubmit: function (e) {
    var that = this;
    var dianpuname = e.detail.value.dianpuname;
    var dianpujieshao = e.detail.value.dianpujieshao;
    var dianpuxingming = e.detail.value.dianpuxingming;
    var dianpuphone = e.detail.value.dianpuphone;
    var dianpuqq = e.detail.value.dianpuqq;
    var dianputel = e.detail.value.dianputel; 
    var dianpuaddress = e.detail.value.dianpuaddress;
    var wx_code = e.detail.value.wx_code;
    if (dianpuname.length <= 0) {
      this.setData({
        showTopTipsText: '请输入店铺名称',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (dianpuname.length <= 0){
      this.setData({
        showTopTipsText: '请输入店铺名称',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else
    if (dianpujieshao.length <= 0) {
      this.setData({
        showTopTipsText: '请输入店铺介绍',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else
    if (dianpuname.length <= 0) {
      this.setData({
        showTopTipsText: '请输入手机号码',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else
      if (dianpuname.length <= 0) {
        this.setData({
          showTopTipsText: '请输入姓名',
          showTopTips: true
        })
        setTimeout(function () {
          that.setData({
            showTopTips: false
          });
        }, 3000);
      }
        else{
      //请求 修改店铺
      wx.request({
        url: baseUrl +'weapp/Store/modi_store_data.html',
        data: {
          user_id: app.globalData.user_id,


          store_name: dianpuname,
          store_desc: dianpujieshao,
          user_name: dianpuxingming,
          store_phone: dianpuphone,
          store_qq: dianpuqq,
          service_phone: dianputel,
          store_address: dianpuaddress,
          store_logo:that.data.images2,
          store_avatar: that.data.images3,
          store_cert: that.data.images1,
          wx_code: wx_code,



        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          console.log(res.data)

          if (res.data.code == 0) {
            wx.showModal({
              title: '提示',
              content: '修改成功',
              showCancel:false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.switchTab({
                    url: '../mine/mine',
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
            
          } else if(res.data.code == -1){
            wx.showModal({
              title: '提示',
              content: '请修改数据',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')

                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }else{
            wx.showModal({
              title: '提示',
              content: '未知错误',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        },
        fail: function () {
        },
        complete: function () {
        }
      })
    }






  },

  //上传图片 显示功能
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {

        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: [],
        })
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        })
        //上传图片
        sendPhotos(res.tempFilePaths, that, app.globalData.user_id, app.globalData.user_id, 1)
      }
    })
  },


  chooseImage2: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files2: [],
        })
        that.setData({
          files2: that.data.files2.concat(res.tempFilePaths)
        })
        //上传图片
        sendPhotos(res.tempFilePaths, that, app.globalData.user_id, app.globalData.user_id, 2)
      }
    })
  },

  chooseImage3: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files3:[],
        })
        that.setData({
          files3:that.data.files3.concat(res.tempFilePaths)
        })
        //上传图片
        sendPhotos(res.tempFilePaths, that, app.globalData.user_id, app.globalData.user_id,3)
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  previewImage2: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id, // 当前显示图片的http链接
      urls: this.data.files2 // 需要预览的图片http链接列表
    })
  },
  previewImage3: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id, // 当前显示图片的http链接
      urls: this.data.files3 // 需要预览的图片http链接列表
    })
  },





 
});


//上传图片
function sendPhotos(arr,that, openid,user_id,images_id) {
  wx.showToast({
    icon: "loading",
    title: "正在上传"
  })

  wx.uploadFile({
    url: baseUrl +'weapp/SavePicApi/save_shoppic.html',
    filePath: arr[0],
    name: 'file',
    formData: {
      //和服务器约定的token, 一般也可以放在header中
      'openid': openid,
      'user_id': user_id
    },
    header: { "Content-Type": "multipart/form-data" },
    success: function (res) {
      console.log(JSON.parse(res.data));
      if (images_id == 1){
        var images1 = JSON.parse(res.data);
        that.setData({
          images1: images1,
        })
      }
      if (images_id == 2) {
        var images2 = JSON.parse(res.data);
        that.setData({
          images2: images2,
        })
      }
      if (images_id == 3) {
        var images3 = JSON.parse(res.data);
        that.setData({
          images3: images3,
        })
      }
      wx.showToast({
        icon: "success",
        title: "上传成功"
      })

    }

  })



}